package com.zh.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.blog.dao.ArticleTagDao;
import com.zh.blog.dao.TagDao;
import com.zh.blog.dto.TagBackedDto;
import com.zh.blog.dto.TagDto;
import com.zh.blog.exception.BizException;
import com.zh.blog.model.ArticleTag;
import com.zh.blog.model.Tag;
import com.zh.blog.service.TagService;
import com.zh.blog.util.BeanCopyUtils;
import com.zh.blog.util.PageUtils;
import com.zh.blog.vo.ConditionVO;
import com.zh.blog.vo.PageResult;
import com.zh.blog.vo.TagVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 博文标签服务
 * @author ZhengHao
 * Created At 2022/8/2.
 **/
@Service
public class TagServiceImpl extends ServiceImpl<TagDao, Tag> implements TagService {
    @Autowired
    private TagDao tagDao;
    @Autowired
    private ArticleTagDao articleTagDao;

    @Override
    public PageResult<TagDto> listTags() {
        // 查询标签列表
        List<Tag> tagList = tagDao.selectList(null);
        // 转换Dto
        List<TagDto> tagDtoList = BeanCopyUtils.copyList(tagList, TagDto.class);
        // 查询标签数量
        Long count = tagDao.selectCount(null);
        return new PageResult<>(tagDtoList, count);
    }

    @Override
    public PageResult<TagBackedDto> listTagBackDto(ConditionVO conditionVO) {
        // 查询标签数量
        Long count = tagDao.selectCount(new LambdaQueryWrapper<Tag>()
        .like(StringUtils.isNotBlank(conditionVO.getKeywords()), Tag::getTagName, conditionVO.getKeywords()));
        if (count == 0 ){
            return new PageResult<>();
        }
        // 分页查询标签列表
        List<TagBackedDto> tagList = tagDao.listTagBackDto(PageUtils.getLimitCurrent(), PageUtils.getSize(), conditionVO);
        return new PageResult<>(tagList, count);
    }

    @Override
    public List<TagDto> listTagsBySearch(ConditionVO conditionVO) {
        // 搜索标签
        List<Tag> tagList = tagDao.selectList(new LambdaQueryWrapper<Tag>()
                .like(StringUtils.isNotBlank(conditionVO.getKeywords()), Tag::getTagName, conditionVO.getKeywords())
                .orderByDesc(Tag::getId));
        return BeanCopyUtils.copyList(tagList,TagDto.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdateTag(TagVO tagVO) {
        // 查询标签名是否存在
        Tag existTag = tagDao.selectOne(new LambdaQueryWrapper<Tag>()
                .select(Tag::getId)
                .eq(Tag::getTagName, tagVO.getTagName()));
        if (Objects.nonNull(existTag) && !existTag.getId().equals(tagVO.getId())){
            throw new BizException("标签名称已存在");
        }
        Tag tag = BeanCopyUtils.copyObject(tagVO, Tag.class);
        this.saveOrUpdate(tag);
    }

    @Override
    public void deleteTag(List<Integer> tagIdList) {
        // 查询标签下是否有文章
        Long count = articleTagDao.selectCount(new LambdaQueryWrapper<ArticleTag>()
                .in(ArticleTag::getTagId, tagIdList));
        if (count > 0){
            throw new BizException("删除失败，该标签下存在文章");
        }
        tagDao.deleteBatchIds(tagIdList);
    }
}